﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Communicate
{
    public class SerialPort
    {
        [DllImport("sms.dll", EntryPoint = "Sms_Connection")]
        public static extern uint Sms_Connection(string CopyRight, uint Com_Port, uint Com_BaudRate, out string Mobile_Type, out string CopyRightToCOM);

        [DllImport("sms.dll", EntryPoint = "Sms_Disconnection")]
        public static extern uint Sms_Disconnection();

        [DllImport("sms.dll", EntryPoint = "Sms_Send")]
        public static extern uint Sms_Send(string Sms_TelNum, string Sms_Text);

        [DllImport("sms.dll", EntryPoint = "Sms_Receive")]
        public static extern uint Sms_Receive(string Sms_Type, out string Sms_Text);

        [DllImport("sms.dll", EntryPoint = "Sms_Delete")]
        public static extern uint Sms_Delete(string Sms_Index);

        [DllImport("sms.dll", EntryPoint = "Sms_AutoFlag")]
        public static extern uint Sms_AutoFlag();

        [DllImport("sms.dll", EntryPoint = "Sms_NewFlag")]
        public static extern uint Sms_NewFlag();

        private bool isOpen;

        private System.Timers.Timer timer;

        public SerialPort()
        {
            timer = new System.Timers.Timer();
            timer.Interval = 2000;
            timer.AutoReset = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timer.Stop();
            string strReceiveSms = string.Empty;
            if (Sms_Receive("0", out strReceiveSms) != 1)
            {
                timer.Start();
                return;
            }
            if (strReceiveSms == null)
            {
                timer.Start();
                return;
            }
            if (this.MessageReceivedEvent != null)
                this.MessageReceivedEvent(strReceiveSms);
            timer.Start();
        }

        public event Action<string> MessageReceivedEvent;

        public bool OpenSerialPort(uint com)
        {
            if (isOpen)
                return true;
            try
            {
                string TypeStr = "";
                string CopyRightToCOM = "";
                string CopyRightStr = "//上海迅赛信息技术有限公司,网址www.xunsai.com//";
                if (Sms_Connection(CopyRightStr, com, 9600, out TypeStr, out CopyRightToCOM) == 1)
                {
                    timer.Start();
                    isOpen = true;
                }
                return isOpen;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool CloseSerialPort()
        {
            Sms_Disconnection();
            this.isOpen = false;
            this.timer.Stop();
            return true;
        }

        public bool SendData(string telNum, string smsText)
        {
            return Sms_Send(telNum, smsText) == 1;
        }

        public bool DeleteData(string smsIndex)
        {
            return Sms_Delete(smsIndex)==1;
        }




    }
}
